export type ResolveType<T> = (value: T | PromiseLike<T>) => void;
export type RejectType = (reason?: any) => void;

export class ETTaskCompleted {

    constructor() {
        return Promise.resolve(true);
    }

}

export class ETTask<T> extends Promise<T> {

    public static ExceptionHandler: Function;

    public static get CompletedTask() {
        return new ETTaskCompleted();
    }

    private _value!: T | PromiseLike<T>;
    private _resolve!: ResolveType<T>;
    private _reject!: RejectType;

    static Create<T>() {
        let tempResolve: ResolveType<T> = null;
        let tempReject: RejectType = null;
        let tcs = new ETTask<T>((resolve: ResolveType<T>, reject: RejectType) => {
            tempResolve = resolve;
            tempReject = reject;
        });
        tcs._resolve = tempResolve;
        tcs._reject = tempReject;
        return tcs;
    }

    public GetResult() {
        return this._value;
    }

    public SetResult(result?: T | PromiseLike<T>) {
        this._value = result;

        let r = this._resolve;
        r?.call(this, result);
    }

    public SetException(e: Error) {
        let r = this._reject;
        r?.call(this, e);
    }

}

export class ETVoid extends ETTask<void> {

    public static get CompletedTask() {
        return new ETTaskCompleted();
    }

}